package cn.edu.zjut.mina.service.impl;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.edu.zjut.mina.bean.RoomInfo;
import cn.edu.zjut.mina.bean.StandardFormat;
import cn.edu.zjut.mina.interf.IRoomInfo;
import cn.edu.zjut.mina.protocal.JMessageProtocalRes;
import cn.edu.zjut.mina.protocal.ProtocalRegulation;
import cn.edu.zjut.mina.service.IBackToHallService;
import cn.edu.zjut.mina.service.INotificationService;
import cn.edu.zjut.mina.util.DateUtils;
import cn.edu.zjut.mina.util.GsonUtils;
import cn.edu.zjut.mina.util.UserSessionMap;
import cn.edu.zjut.mina.util.ResultInfoConstUtils;
@Service("backToHallService")
public class BackToHallServiceImpl implements IBackToHallService{
	//test
	/**
	 * 用于存放用户信息的集合
	 */
	private final UserSessionMap userSessionMap = UserSessionMap.getInstance();
	
	@Qualifier("notificationService")
	@Autowired
	private INotificationService notificationService;
	
	public JMessageProtocalRes backToHall(StandardFormat inJson,
			IoSession session) {
		// TODO Auto-generated method stub
		System.out.println(DateUtils.getTime() + "用户" + session.getAttribute("user") + "返回大厅");
		JMessageProtocalRes res = new JMessageProtocalRes();
		StandardFormat outJson = new StandardFormat();
		String datas;
		IRoomInfo roomInfo = userSessionMap.get(inJson.roomId);
		
		if (roomInfo == null) {
			System.out.println(DateUtils.getTime() + "用户" + session.getAttribute("user") + "返回大厅：房间号为空");
			outJson.userId = inJson.userId;
			outJson.resultInfo = ResultInfoConstUtils.BACKTOHALL_ROOMNULLINFO;
			outJson.result = false;
		} else {
			synchronized(roomInfo) {//参考java编程思想 689页
				if (roomInfo.getParticipation() == roomInfo.getActualPrepare() || 
						roomInfo.getActual_number_of_games() > 0) {//第一局的时候所有人都准备了或者游戏开始了则不允许返回大厅
					outJson.userId = inJson.userId;
					outJson.resultInfo = ResultInfoConstUtils.BACKTOHALL_FAIL1INFO;
					outJson.result = false;
				} else {
					if (roomInfo.getUserSessions().get(0).user.getUserId().equals(inJson.userId)) {//庄家返回大厅显示离线
						roomInfo.getUserSessions().get(0).user.setPrepare(0);
						roomInfo.getUserSessions().get(0).user.setLostConnection(1);
						roomInfo.setActualPrepare(roomInfo.getActualPrepare() - 1);//准备人数减一
						notificationService.notificationBackToHaLL(inJson.roomId, inJson.userId);
						return null;
					} else {//闲家返回大厅则是直接退出房间
						int i = 1;
						for (; i < roomInfo.getUserSessions().size(); i++) {
							if (roomInfo.getUserSessions().get(i).user.getUserId().equals(inJson.userId)) {
								break;
							}
						}
						if (i < roomInfo.getUserSessions().size()) {
							roomInfo.setActualParticipation(roomInfo.getActualParticipation() - 1);//参与人数减一
							if (roomInfo.getUserSessions().get(i).user.getPrepare() != null && roomInfo.getUserSessions().get(i).user.getPrepare() == 1) {
								roomInfo.getUserSessions().get(i).user.setPrepare(0);
								roomInfo.setActualPrepare(roomInfo.getActualPrepare() - 1);//准备人数减一
							}
							
							notificationService.notificationBackToHaLL(inJson.roomId, inJson.userId);
							roomInfo.getUserSessions().get(i).user.clear();//玩家变成普通用户
							roomInfo.getUserSessions().remove(i);//移除该玩家
							return null;
						} else {
							outJson.userId = inJson.userId;
							outJson.resultInfo = ResultInfoConstUtils.BACKTOHALL_FAIL2INFO;
							outJson.result = false;
						}
						
					}
				}
			}
		}
		
		res.setResultCode(ProtocalRegulation.PREPAREINITIATIVE);
		datas = GsonUtils.beanToJson(outJson).trim();
		res.setContent(datas);
		return res;
	}
	
}
